# Contributor: Patrycja Rosa <alpine@ptrcnull.me>
# Maintainer: Patrycja Rosa <alpine@ptrcnull.me>
pkgname=wasmtime
pkgver=13.0.0
pkgrel=0
pkgdesc="Fast and secure runtime for WebAssembly"
url="https://wasmtime.dev/"
# others unsupported
arch="aarch64 x86_64"
license="Apache-2.0"
depends_dev="libwasmtime=$pkgver-r$pkgrel"
makedepends="
	cargo
	cargo-auditable
	chrpath
	rust-wasm
	zstd-dev
	"
subpackages="libwasmtime $pkgname-dev"
source="https://github.com/bytecodealliance/wasmtime/releases/download/v$pkgver/wasmtime-v$pkgver-src.tar.gz
	fix-c-api-build.patch
	system-zstd.patch
	"
builddir="$srcdir/wasmtime-v$pkgver-src"
# net: fetch dependencies
# check: custom_limiter_detect_os_oom_failure fails with oom for some reason
options="!check net"


prepare() {
	default_prepare

	git init -q
	# can't patch deps with vendor dir
	rm -fv .cargo/config.toml
	rm -rf vendor

	# Rust target triple.
	local target=$(rustc -vV | sed -n 's/host: //p')

	# Build against system-provided libs
	mkdir -p .cargo
	cat >> .cargo/config.toml <<-EOF
		[target.$target]
		zstd = { rustc-link-lib = ["zstd"] }
	EOF

	cargo fetch --target="$CTARGET" --locked
}

build() {
	cargo auditable build --frozen --release
	cargo auditable build --frozen --release --manifest-path crates/c-api/Cargo.toml
}

check() {
	cargo test --frozen
}

package() {
	install -Dm755 target/release/wasmtime -t "$pkgdir"/usr/bin/
	# remove gigantic useless rpath to /usr/lib
	chrpath -d target/release/libwasmtime.so
	install -Dm644 target/release/libwasmtime.so -t "$pkgdir"/usr/lib/
	cp -a crates/c-api/include "$pkgdir"/usr/
}

libwasmtime() {
	amove usr/lib
}

sha512sums="
bee63653fd800b6596ffc1cc1ded4b018f1ae5f8a989fa38438c554705f7b8c5f220aae8d319fe7ef37b8856887c1ae10c2da12c4212ec4136c96b37807dbb42  wasmtime-v13.0.0-src.tar.gz
8d31c56f5ed5758c00fff5e1e8d8089dc31dddcfaf43fb40e12fea997beeaec57448b19a2ca53bb62268d2825abd6dddc9ed2fec02459415b28da0f5bc60ecd0  fix-c-api-build.patch
5441687022cee3a4774052cf81aef786bfb6385f0474c13964320c529b9f82128063f088c6a6e3e3f2883b592b118f13e0dacb309697d0e8d8e2d9670e33bba7  system-zstd.patch
"
